import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jm_flutter_common/jm_flutter_common.dart';
import '../../../../common/utils/show.dart';
import '../../../widget/widget.dart';

class JmLogDescItem extends StatelessWidget {
  final JmLogDesc logDesc;
  final bool showTime;
  final double textSize;
  const JmLogDescItem(
      {super.key,
      required this.logDesc,
      this.showTime = false,
      this.textSize = 12});

  bool get error => logDesc.level.index <= LogLevel.error.index;
  bool get warn => logDesc.level.index <= LogLevel.warn.index;

  Color get color {
    switch (logDesc.level) {
      case LogLevel.fatal:
        return Colors.redAccent;
      case LogLevel.error:
        return Colors.redAccent;
      case LogLevel.warn:
        return Colors.yellowAccent;
      case LogLevel.verbose:
        return Colors.white;
      case LogLevel.debug:
        return Colors.greenAccent[100]!;
      default:
        return Colors.white;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(
          border: error ? Border.all(color: color, width: 0.5) : null,
        ),
        margin: error
            ? const EdgeInsets.only(top: 2, bottom: 2)
            : const EdgeInsets.only(top: 1, bottom: 1),
        padding: const EdgeInsets.only(top: 1, bottom: 1),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            showTime
                ? Container(
                    width: 82,
                    alignment: Alignment.topLeft,
                    child: MyText(
                      logDesc.time.toLogShort,
                      size: textSize,
                      color: color,
                      i18n: false,
                    ),
                  )
                : Container(),
            Flexible(
              child: MyText(
                logDesc.msg,
                size: textSize,
                color: color,
                weight: warn ? FontWeight.bold : FontWeight.normal,
                i18n: false,
              ),
            ),
          ],
        ),
      ),
      onLongPress: () {
        Clipboard.setData(ClipboardData(text: logDesc.msg));
        Show.toast("已复制到剪切板");
      },
    );
  }
}
